class HUD
  MAGAZINE_BAR_HEIGHT = 16
  def initialize(window, unit, x, y)
    @window, @unit, @x, @y = window, unit, x, y
    @orientation, @color = unit.hud_orientation, unit.color_uniform
    @font = Gosu::Font.new(window, Gosu::default_font_name, 16)
  end
  
  def update
  end
  
  def draw
    temp_y = @y
    
    health_text = "Health: #{@unit.health.to_i}"
    score_text = "Score: #{@unit.score}"
    weapon_text = "Weapon: #{@unit.selected_weapon.name}"
    
    case @orientation
      when :left
        rel_x = 0
        raise if @unit.selected_weapon.magazine_max == nil
        if @unit.selected_weapon.reload_timer > 0
          magazine_bar_x2 = @x + (@unit.selected_weapon.reload_time - @unit.selected_weapon.reload_timer) / @unit.selected_weapon.reload_time.to_f * 100
        else
          magazine_bar_x2 = @x + @unit.selected_weapon.magazine.to_f / @unit.selected_weapon.magazine_max * 100
        end
        magazine_full_bar_x2 = @x + 100
      when :right
        rel_x = 1
        if @unit.selected_weapon.reload_timer > 0
          magazine_bar_x2 = @x - (@unit.selected_weapon.reload_time - @unit.selected_weapon.reload_timer) / @unit.selected_weapon.reload_time.to_f * 100
        else
          magazine_bar_x2 = @x - @unit.selected_weapon.magazine.to_f / @unit.selected_weapon.magazine_max * 100
        end
        magazine_full_bar_x2 = @x - 100
      end
      
      if @unit.health > 80
        health_color = 0xff00cc00
      elsif @unit.health > 30
        health_color = 0xffffff00
      else
        health_color = 0xffcc0000
      end
      
      @font.draw_rel(health_text, @x, temp_y, ZOrder::HUD, rel_x, 0, 1, 1, health_color)
      temp_y += 32
      @font.draw_rel(score_text, @x, temp_y, ZOrder::HUD, rel_x, 0)
      temp_y += 32
      
      @font.draw_rel(weapon_text, @x, temp_y, ZOrder::HUD, rel_x, 0)
      temp_y += 32
      
      $game.draw_quad(@x, temp_y, @color, 
                          magazine_bar_x2, temp_y, @color,
                          @x, temp_y + MAGAZINE_BAR_HEIGHT, @color,
                          magazine_bar_x2, temp_y + MAGAZINE_BAR_HEIGHT, @color, ZOrder::HUD)
      
      
      @magazine_full_color = @color - 256**4/2
      $game.draw_quad(@x, temp_y, @magazine_full_color,
                          magazine_full_bar_x2, temp_y, @magazine_full_color,
                          @x, temp_y + MAGAZINE_BAR_HEIGHT, @magazine_full_color,
                          magazine_full_bar_x2, temp_y + MAGAZINE_BAR_HEIGHT, @magazine_full_color, ZOrder::HUD)
      
      if @unit.final_revenge
        temp_y += 32
        @font.draw_rel("Final revenge!", @x, temp_y, ZOrder::HUD, rel_x, 0) 
      end
      
      if @unit.poisoned
        temp_y += 32
        @font.draw_rel("Poisoned", @x, temp_y, ZOrder::HUD, rel_x, 0, 1, 1, 0xff7fdc7f)
      end
      
      if @orientation == :right
        temp_y += 32
        @font.draw_rel("Update time: #{$game.update_time}", @x, temp_y, ZOrder::HUD, rel_x, 0)
      end
  end
end
